package com.youan.pangu.security.brower.controller;

import com.youan.pangu.base.bean.GlobalValue;
import com.youan.pangu.base.bean.result.ErrorResponse;
import com.youan.pangu.base.bean.result.ErrorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: 辛晟昊
 * @time: 2020/3/8 15:37
 */
@Controller
@RequestMapping("/authentication")
public class AuthenticationController {
    private Logger logger = LoggerFactory.getLogger(GlobalValue.LOOGER_NAME);

    // 封装了引发跳转请求的工具类，看实现类应该是从session中获取的
    private RequestCache requestCache = new HttpSessionRequestCache();

    @RequestMapping("/requireLogin")
    public ErrorResponse requireLogin(HttpServletRequest request, HttpServletResponse response){
        SavedRequest savedRequest = requestCache.getRequest(request, response);
        // 如果有引发认证的请求
        if (savedRequest != null) {
            String targetUrl = savedRequest.getRedirectUrl();
            logger.warn("请求<{}>未登录", targetUrl);
        }
        return new ErrorResponse("请登录", ErrorType.NOLOGIN_ERROR);
    }
}
